//
//  YblbVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/9.
//

import UIKit

class YblbVC: BaseVC {
    
    var type: String = ""
    
    var id: String = ""
    
    var collectionView: UICollectionView!
    var collectionView2: UICollectionView!
    
    var dataArray: [[String: Any]] = []
    var dataArray2: [[String: Any]] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        collectionView?.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(getData))
        collectionView2?.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(getData))
        getData()
    }
    
    // 数据刷新
    @objc func getData() {
        if type == "标注" {
            let paramet: [String: String] = [
                "encryptedData": "zFoamlSX95gBI7S05+e6HakuGltzhbckTNdkY0yZ2hU="
            ]
            EWERequest.lgf("label/" + id, paramet, .get) { [weak self] (data) in
                self?.dataArray = data["sample"] as? [[String: Any]] ?? []
                self?.dataArray2 = data["labelUserSamples"] as? [[String: Any]] ?? []
                self?.collectionView.reloadData()
                self?.collectionView2.reloadData()
                self?.endRefreshing()
            } _: { [weak self] (error) in
                SVProgressHUD.showError(error)
                self?.endRefreshing()
            }
        } else {
            let paramet: [String: String] = [
                "encryptedData": "zFoamlSX95gBI7S05+e6HakuGltzhbckTNdkY0yZ2hU="
            ]
            EWERequest.lgf("audit/" + id, paramet, .get) { [weak self] (data) in
                self?.dataArray = data["sample"] as? [[String: Any]] ?? []
                self?.collectionView.reloadData()
                self?.endRefreshing()
            } _: { [weak self] (error) in
                SVProgressHUD.showError(error)
                self?.endRefreshing()
            }
        }
    }

    func endRefreshing() {
        self.collectionView?.mj_header?.endRefreshing()
        self.collectionView?.mj_footer?.endRefreshing()
        self.collectionView2?.mj_header?.endRefreshing()
        self.collectionView2?.mj_footer?.endRefreshing()
    }

}

extension YblbVC : UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "YblbCellPlayerStop"), object: nil)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  {
        if collectionView == self.collectionView {
            return CGSize.init(width: UIDevice.lgf_ScreenW, height: 120)
        }
        return CGSize.init(width: UIDevice.lgf_ScreenW, height: 80)
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.collectionView {
            return dataArray.count
        }
        return dataArray2.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.collectionView {
            let cell = collectionView.lgf_DequeueReusableCell(with: YblbCell.self, for: indexPath)
            cell.tzzx.text = (dataArray[indexPath.item]["no"] as? String) ?? ""
            cell.url = ((dataArray[indexPath.item]["sampleDetail"] as? [String: Any])?["wavPath"] as? String) ?? ""
            cell.initPlay()
            cell.allTime = cell.player.getAllTime(((dataArray[indexPath.item]["sampleDetail"] as? [String: Any])?["duration"] as? Int) ?? 0)
            cell.play_time.text = "00:00/" + cell.allTime
            return cell
        }
        let cell = collectionView.lgf_DequeueReusableCell(with: Yblb2Cell.self, for: indexPath)
        cell.tzzx.text = (dataArray2[indexPath.item]["labelName"] as? String) ?? ""
        let status = (dataArray2[indexPath.item]["status"] as? Int) ?? 10
        // 10:标注开始；20：标注完成;30:标注超时
        if status == 10 {
            cell.status.text = "标注开始"
            cell.status.backgroundColor = UIColor.init(lgf_HexString: "F27C0F")?.withAlphaComponent(0.1)
            cell.status.textColor = UIColor.init(lgf_HexString: "F27C0F")
            cell.status.layer.borderColor = UIColor.init(lgf_HexString: "F27C0F")?.withAlphaComponent(0.2).cgColor
        } else if status == 20 {
            cell.status.text = "标注完成"
            cell.status.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")?.withAlphaComponent(0.1)
            cell.status.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.status.layer.borderColor = UIColor.init(lgf_HexString: "0F88F2")?.withAlphaComponent(0.2).cgColor
        } else if status == 30 {
            cell.status.text = "标注超时"
            cell.status.backgroundColor = UIColor.init(lgf_HexString: "DE6161")?.withAlphaComponent(0.1)
            cell.status.textColor = UIColor.init(lgf_HexString: "DE6161")
            cell.status.layer.borderColor = UIColor.init(lgf_HexString: "DE6161")?.withAlphaComponent(0.2).cgColor
        }
        return cell
    }
}
